﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO.Admin;
using System.Data;

namespace ProjectQuaDep.Admin.News
{
    public partial class EditZone : System.Web.UI.Page
    {
        string lv = "1";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                showListMenuLv1();
                string id = Request["id"] == null ? "0" : Request["id"].ToString();
                lv = Request["lv"] == null ? "1" : Request["lv"].ToString();
                string type = Request["type"] == null ? "add" : Request["type"].ToString();
                lblLV.Text = lv;
                if (type == "add")
                {
                    //phan them moi
                    cmdUpdate.Text = "Thêm mới";
                    if (lv == "1")
                    {
                        ddlMenu.Visible = false;
                    }
                }
                else
                {
                    //phan edit
                    cmdUpdate.Text = "Sửa chữa";
                    setInfomenu(id);
                    if (lv == "1")
                    {
                        ddlMenu.Visible = false;
                    }
                    else
                    {
                        //tro den me
                        string parentId = Request["parentId"] == null ? "1" : Request["parentId"].ToString();
                        ddlMenu.SelectedValue = parentId;
                    }
                }
            }
        }
        protected void setInfomenu(string zoneid)
        {
            DataTable dt = new DataTable();
            dt = NewsBO.getInfomationZoneByIDCache(zoneid);
            if (dt.Rows.Count > 0)
            {
                lblID.Text = zoneid;
                txtTitle.Text = dt.Rows[0][BO.Const.tbZoneProduct.title].ToString();
                string sSelect = dt.Rows[0][BO.Const.tbZoneProduct.status].ToString();
                BO.Utils.setOneSelect(ckSelect, sSelect);
            }
        }
        protected void showListMenuLv1()
        {
            ddlMenu.DataSource = NewsBO.getAllZoneCache();
            ddlMenu.DataTextField = BO.Const.tbZoneProduct.title;
            ddlMenu.DataValueField = BO.Const.tbZoneProduct.ZoneId;
            ddlMenu.DataBind();
        }
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            string ParentId = ddlMenu.SelectedValue.ToString();
            if (lblLV.Text == "1")
            {
                ParentId = "0";
            }
            if (cmdUpdate.Text == "Thêm mới")
            {
                //them
                string kq = NewsBO.addZoneBO(txtTitle.Text, BO.Utils.BuildLinkUrlNice(txtTitle.Text.Trim()), ParentId, BO.Utils.getOneSelect(ckSelect));
                if (kq == "1")
                {
                    Response.Redirect("/Admin/News/ManagerCategory.aspx");
                }
                else
                {
                    lblMs.Text = "Thêm mới thất bại";
                }
            }
            else
            {
                //sua
                string kq = NewsBO.editZoneBO(txtTitle.Text, BO.Utils.BuildLinkUrlNice(txtTitle.Text.Trim()), ParentId, BO.Utils.getOneSelect(ckSelect), lblID.Text);
                if (kq == "1")
                {
                    Response.Redirect("/Admin/News/ManagerCategory.aspx");
                }
                else
                {
                    lblMs.Text = "Sửa chữa thất bại";
                }
            }
        }
    }
}